View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanReader.java,v 1.5 2002/10/24 11:13:22 jstrachan Exp $ 3 * $Revision: 1.5 $ 4 * $Date: 2002/10/24 11:13:22 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: BeanReader.java,v 1.5 2002/10/24 11:13:22 jstrachan Exp $ 61 */ 62 package org.apache.commons.betwixt.io; 63 64 import java.beans.IntrospectionException; 65 import java.util.HashSet; 66 import java.util.Iterator; 67 import java.util.Set; 68 69 import javax.xml.parsers.SAXParser; 70 71 import org.apache.commons.betwixt.AttributeDescriptor; 72 import org.apache.commons.betwixt.ElementDescriptor; 73 import org.apache.commons.betwixt.XMLBeanInfo; 74 import org.apache.commons.betwixt.XMLIntrospector; 75 import org.apache.commons.betwixt.expression.Context; 76 import org.apache.commons.betwixt.expression.Expression; 77 78 import org.apache.commons.digester.Digester; 79 import org.apache.commons.digester.Rule; 80 81 import org.apache.commons.logging.Log; 82 import org.apache.commons.logging.LogFactory; 83 84 import org.xml.sax.XMLReader; 85 86 /*** <p><code>BeanReader</code> reads a tree of beans from an XML document.</p> 87 * 88 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 89 * @version $Revision: 1.5 $ 90 */ 91 public class BeanReader extends Digester { 92 93 /*** Introspector used */ 94 private XMLIntrospector introspector = new XMLIntrospector(); 95 /*** Log used for logging (Doh!) */ 96 private Log log = LogFactory.getLog( BeanReader.class ); 97 /*** The registered classes */ 98 private Set registeredClasses = new HashSet(); 99 /*** Should the reader use <code>ID</code>'s to match */ 100 private boolean matchIDs = true; 101 102 /*** 103 * Construct a new BeanReader with default properties. 104 */ 105 public BeanReader() { 106 } 107 108 /*** 109 * Construct a new BeanReader, allowing a SAXParser to be passed in. This 110 * allows BeanReader to be used in environments which are unfriendly to 111 * JAXP1.1 (such as WebLogic 6.0). Thanks for the request to change go to 112 * James House (james@interobjective.com). This may help in places where 113 * you are able to load JAXP 1.1 classes yourself. 114 */ 115 public BeanReader(SAXParser parser) { 116 super(parser); 117 } 118 119 /*** 120 * Construct a new BeanReader, allowing an XMLReader to be passed in. This 121 * allows BeanReader to be used in environments which are unfriendly to 122 * JAXP1.1 (such as WebLogic 6.0). Note that if you use this option you 123 * have to configure namespace and validation support yourself, as these 124 * properties only affect the SAXParser and emtpy constructor. 125 */ 126 public BeanReader(XMLReader reader) { 127 super(reader); 128 } 129 130 131 /*** 132 * Registers a bean class which is used by the reader 133 * to deduce the digester rules. 134 */ 135 public void registerBeanClass(Class beanClass) throws IntrospectionException { 136 if ( ! registeredClasses.contains( beanClass ) ) { 137 registeredClasses.add( beanClass ); 138 139 // introspect and find the ElementDescriptor to use as the root 140 XMLBeanInfo xmlInfo = introspector.introspect( beanClass ); 141 ElementDescriptor elementDescriptor = xmlInfo.getElementDescriptor(); 142 143 String path = elementDescriptor.getQualifiedName(); 144 if (log.isTraceEnabled()) { 145 log.trace("Added path: " + path + ", mapped to: " + beanClass.getName()); 146 } 147 addBeanCreateRule( path, elementDescriptor, beanClass ); 148 addBeanCreateRule( "*/" + path, elementDescriptor, beanClass ); 149 } 150 } 151 152 /*** 153 * Registers a bean class at the given path expression 154 * which is used by the reader to deduce the digester rules. 155 */ 156 public void registerBeanClass(String path, Class beanClass) throws IntrospectionException { 157 if ( ! registeredClasses.contains( beanClass ) ) { 158 registeredClasses.add( beanClass ); 159 160 // introspect and find the ElementDescriptor to use as the root 161 XMLBeanInfo xmlInfo = introspector.introspect( beanClass ); 162 ElementDescriptor elementDescriptor = xmlInfo.getElementDescriptor(); 163 164 addBeanCreateRule( path, elementDescriptor, beanClass ); 165 } else { 166 log.warn("Cannot add class " + beanClass.getName() + " since it already exists"); 167 } 168 } 169 170 // Properties 171 //------------------------------------------------------------------------- 172 173 /*** 174 * <p> Get the introspector used. </p> 175 * 176 * <p> The {@link XMLBeanInfo} used to map each bean is created by the <code>XMLIntrospector</code>. 177 * One way in which the mapping can be customized is by altering the <code>XMLIntrospector</code>. </p> 178 */ 179 public XMLIntrospector getXMLIntrospector() { 180 return introspector; 181 } 182 183 184 /*** 185 * <p> Set the introspector to be used. </p> 186 * 187 * <p> The {@link XMLBeanInfo} used to map each bean is created by the <code>XMLIntrospector</code>. 188 * One way in which the mapping can be customized is by altering the <code>XMLIntrospector</code>. </p> 189 * 190 * @param introspector use this introspector 191 */ 192 public void setXMLIntrospector(XMLIntrospector introspector) { 193 this.introspector = introspector; 194 } 195 196 /*** 197 * <p> Get the current level for logging. </p> 198 * 199 * @return a <code>org.apache.commons.logging.Log</code> level constant 200 */ 201 public Log getLog() { 202 return log; 203 } 204 205 /*** 206 * <p> Set the current logging level. </p> 207 * 208 * @param level a <code>org.apache.commons.logging.Log</code> level constant 209 */ 210 public void setLog(Log log) { 211 this.log = log; 212 setLogger(log); 213 } 214 215 /*** 216 * Should the reader use <code>ID</code> attributes to match beans. 217 */ 218 public boolean getMatchIDs() { 219 return matchIDs; 220 } 221 222 /*** 223 * Set whether the read should use <code>ID</code> attributes to match beans. 224 */ 225 public void setMatchIDs(boolean matchIDs) { 226 this.matchIDs = matchIDs; 227 } 228 229 // Implementation methods 230 //------------------------------------------------------------------------- 231 232 /*** 233 * Adds a new bean create rule for the specified path 234 */ 235 protected void addBeanCreateRule( String path, ElementDescriptor elementDescriptor, Class beanClass ) { 236 Rule rule = new BeanCreateRule( elementDescriptor, beanClass, path + "/" , matchIDs); 237 addRule( path, rule ); 238 239 if ( log.isDebugEnabled() ) { 240 log.debug( "Added root rule to path: " + path + " rule: " + rule ); 241 } 242 } 243 244 }

This page was automatically generated by Maven